// number.js

var inType = "";//money,signMoney,rate,signRate,shortRate,money4,rate100,rate63
function DoInit(iType)
{
    var sTag = tagName.toLowerCase();
    inType = iType;

	this.style.textAlign = "right";

	if (sTag == "input" || sTag == "textarea" ) 
    {
		attachEvent("onkeypress",doKeyPress);
		attachEvent("onfocus",doFocus);
		attachEvent("onkeydown",doKeyDown);
		attachEvent("onmouseup",doMouseUp);
		attachEvent("onkeyup",doKeyUp);
		
		if(this.value == "0" || this.value == "0.00" || this.value=="0.0" || this.value=="0."
			|| this.value=="0.000" || this.value=="0.0000"
			|| this.value=="0.00000" || this.value=="0.000000" ){
			
			this.value="0";
		}else if((inType=="money"||inType=="signMoney") && this.value!="" ){
			chnumber(this);
			if(this.value=="")return;
			var a=this.value.indexOf('.');
			var l=this.value.length;
			if( a<0) this.value+='.00';
			else if(l-a==2) this.value+='0';
			else if(l-a==1) this.value+='00';
		}
    }
}

function doKeyPress() {
	formatNumber(this);
}

function doFocus() {
	setCaretAtEnd(this);
}

function doKeyDown() {
	chKey(this);
}

function doKeyUp() {
	setCaretAtEnd(this);
	chnumber(this);
}

function doMouseUp() {
	setCaretAtEnd(this);
	chnumber(this);
}

function setCaretAtEnd(field)  {
   if  (field.createTextRange)  {
       var  r  =  field.createTextRange();
       r.moveStart('character',  field.value.length);
       r.collapse();
       r.select();
   }
}

function chnumber(varthis){
	var a,renum='';
	var j=0;
	var a1='',a2='',a3='';
	var tes=/^-/;
	if(event.keyCode>=33&&event.keyCode<=40) return;

	a=unchnumber(varthis.value);
	a=rmChar(a);//????????????
	a=a.replace(/(^\s*)|(\s*$)/g, "");//trim
	//j=a.indexOf('-');
	if(tes.test(a)) a1='-';
	else a1='';//a1=a.substr(0,++j);
	//a=a.substr(j);
	a=a.replace(/-/g,"");
	if(a!="0" && a.substr(0,2)!="0.")a=a.replace(/^0*/g, "");
	j=a.indexOf('.');
	if(j<0) j=a.length;
	a2=a.substr(0,j);
	a3=a.substr(j);
	j=0;
	for( i=a2.length;i>3;i=i-3){
		renum=","+a2.substr(i-3,3)+renum;
		j++;
	}
	renum=a1+a2.substr(0,a2.length-j*3)+renum+a3;
	varthis.value=renum;
}

function formatNumber(field)  {
	var C = '';
	if(inType=="rate"){
		rTest = /\.\d{6}/;
		sTest = /\d{2}/;
		zTest = /\.\d{5}/;
		signed = false;
	}
	else if(inType=="rate63"){
		rTest = /\.\d{3}/;
		sTest = /\d{2}/;
		zTest = /\.\d{2}/;
		signed = false;
	}
	else if(inType=="rate100"){
		rTest = /\.\d{5}|^100/;
		sTest = /\d{2}/;
		zTest = /\.\d{4}|^100/;
		signed = false;
	}
	else if(inType=="signRate"){
		rTest = /\.\d{6}/;
		sTest = /\d{2}/;
		zTest = /\.\d{5}/;
		signed = true;
	}
	else if(inType=="shortRate"){
		rTest = /\.\d{2}/;
		sTest = /\d{2}/;
		zTest = /\.\d{1}/;
		signed = true;
	}
	else if(inType=="money4"){
		rTest = /\.\d{4}/;//????????
		sTest = /\d{2},\d{3},\d{3},\d{3}/;//????????
		zTest = /\.\d{3}/;//??????????
		signed = false;
	}
	else{
		rTest = /\.\d{2}/;//????????
		sTest = /\d{1},\d{3},\d{3},\d{3},\d{3}/;//????????
		zTest = /\.\d{2}/;//??????????
		signed = (inType != "money");
	}

	field.value=field.value.replace(/(^\s*)|(\s*$)/g, "");
	if (event.keyCode == 8 ) {
      event.keyCode == 8;
	  event.returnValue = true;
	  return;
    }
	else if (event.srcElement.tagName != "TEXTAREA" && event.keyCode == 13) {
	  event.keyCode = 9;
      event.returnValue = true;
	  return;
    }
	else if (event.keyCode==46 && field.value.indexOf('.')==-1) {//'.'
		  if(field.value=="") C='0.';
		  else C = '.';
		  event.returnValue = false;
    }
	else if (event.keyCode == 45 && signed) {//'-'
          if (field.value.indexOf('-')==-1) field.value = '-' + field.value;
		  else field.value=field.value.replace(/-/,"");
		  event.returnValue = false;
    }
	else if (event.keyCode == 48 && field.value!="0" && field.value!="-0" ) {
          if((!zTest.test(field.value) && (!sTest.test(field.value) || field.value.indexOf('.')>-1))
          	|| (field.value=="10" && inType=="rate100"))
				C = String.fromCharCode(event.keyCode);
		  event.returnValue = false;
    }
	else if (event.keyCode > 48 && event.keyCode <= 57 ) {
          if(!rTest.test(field.value) && (!sTest.test(field.value) || field.value.indexOf('.')>-1))
				C = String.fromCharCode(event.keyCode);
		  event.returnValue = false;
    }
    else if (event.keyCode==9){
    	event.returnValue=true;
    	return;
    }
	else {
      event.returnValue = false;
    }

	field.value += C;
	setCaretAtEnd(field);
	chnumber(field);
}

function unchnumber(input_je){
	var	a='',a1='';
	var	j=0;
	a=input_je;
	while((j=a.indexOf(','))>=0){
		a1 += a.substr(0,j);
		a=a.substr(++j);
	}
	a =a1+a;
	return a;
}

function rmChar(input_je){
	var s=input_je;

	s=s.replace(/[^-\.,0-9]/g,"");

	return s;
}

function chKey(field,Arr)  {  
//alert("keyUp:"+event.keyCode);
	if (event.keyCode == 8) {
      event.keyCode == 8;
	  event.returnValue = true;
	  return;
    }
    else if (event.keyCode==9){
    	event.returnValue=true;
    	return;
    }
	if (event.keyCode == 46) {
	  field.value="0";
	  event.returnValue = false;
	  return;
    }
	else if (event.srcElement.tagName != "TEXTAREA" && event.keyCode == 13) {
	  event.keyCode = 9;
      event.returnValue = true;
	  return;
    }
	else if ((event.keyCode==110 || event.keyCode==190) && field.value.indexOf('.')==-1) {//'.'
          event.returnValue = true;
    }
    else if (event.keyCode == 109 || event.keyCode == 189) {//'-'
          event.returnValue = true;
    }
    else if (event.keyCode >= 48 && event.keyCode <= 57 ) {
          event.returnValue = true;
    }
    else if (event.keyCode >= 33 && event.keyCode <= 40 && Arr==0) {
          event.returnValue = true;
    }
    else if (event.keyCode == 229 ) {
          event.returnValue = true;
    }
    else if (event.keyCode >= 96 && event.keyCode <= 105 ) {
          event.returnValue = true;
    }
	else {
      event.returnValue = false;
    }
}
function  ForDight(num,n)  
{	var  dd=1;  
	var  tempnum;  
	for(i=0;i<n;i++)  
	{  
		dd*=10;  
	}  
	tempnum=num*dd;  
	tempnum=Math.round(tempnum);  
	return (tempnum/dd);  
}
